#include<stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include<stdlib.h>
#include<string.h>

#pragma comment(lib, "Ws2_32.lib")



int main(void)
{
	int len;
	struct sockaddr_in address;
	int result;
	int sockfd;

	WSADATA wsa_data;
	if (WSAStartup(MAKEWORD(2, 2), &wsa_data) == -1) {
		perror("Startup");
		return -1;
	}
	if (LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) {
		WSACleanup();
		perror("Could not find a usable version of Winsock.dll");
		return -1;
	}

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	address.sin_family = AF_INET;
	inet_pton(AF_INET, "192.168.33.92", (void*)&(address.sin_addr));
	address.sin_port = htons(11021);
	len = sizeof(address);

	result = connect(sockfd, (struct sockaddr*)&address, len);
	if (-1 == result)
	{
		perror("oops: client1");
		exit(1);
	}


	//wait server clinet sockdfd
	Sleep(5000);

	{
		char str[] = "Hello, world";
		int len = strlen(str);
		int writelen = send(sockfd, str, len, 0);
		printf("1 writlen = %d\n", writelen);
		//will rise a SIGPIPE
		writelen = send(sockfd, str, len, 0);
		printf("2 writlen = %d\n", writelen);
	}

	printf("client done, exit\n");
	closesocket(sockfd);
	WSACleanup();
	return 0;
}
